package com.cyanite.playlv2;

import com.cyanite.playlv2.widget.CategoryPickerAdapter;
import com.cyanite.playlv2api.types.VenueCategory;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Presents the user with a list of all available categories from playlv2api
 * that they can use to label a new venue.
 *
 */
public class CategoryPickerDialog extends Dialog {

    private Group<VenueCategory> mCategories;
    private ViewFlipper mViewFlipper;
    private VenueCategory mChosenCategory;

    private int mFirstDialogHeight;

    public CategoryPickerDialog(Context context, Group<VenueCategory> categories, PlayLv2 application) {
        super(context);
        mCategories = categories;
        mChosenCategory = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.category_picker_dialog);
        setTitle(getContext().getResources().getString(R.string.category_picket_dialog_title));

        mViewFlipper = (ViewFlipper) findViewById(R.id.categoryPickerViewFlipper);

        mFirstDialogHeight = -1;
        mViewFlipper.addView(makePage(mCategories));
    }

    private View makePage(Group<VenueCategory> categories) {

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.category_picker_page, null);

        CategoryPickerPage page = new CategoryPickerPage();
        page.ensureUI(view, mPageListItemSelected, categories);
        view.setTag(page);

        if (mViewFlipper.getChildCount() == 1 && mFirstDialogHeight == -1) {
            mFirstDialogHeight = mViewFlipper.getChildAt(0).getHeight();
        }
        if (mViewFlipper.getChildCount() > 0) {
            view.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, mFirstDialogHeight));
        }

        return view;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mViewFlipper.getChildCount() > 1) {
                    mViewFlipper.removeViewAt(mViewFlipper.getChildCount() - 1);
                    return true;
                }
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * After the user has dismissed the dialog, the parent activity can use this
     * to see which category they picked, if any. Will return null if no
     * category was picked.
     */
    public VenueCategory getChosenCategory() {
        return mChosenCategory;
    }

    private static class CategoryPickerPage {
        private CategoryPickerAdapter mListAdapter;
        private PageListItemSelected mClickListener;

        public void ensureUI(View view, PageListItemSelected clickListener, Group<VenueCategory> categories) {
            mClickListener = clickListener;
            mListAdapter = new CategoryPickerAdapter(view.getContext(), categories);

            ListView listview = (ListView) view.findViewById(R.id.categoryPickerListView);
            listview.setAdapter(mListAdapter);
            listview.setOnItemClickListener(mOnItemClickListener);

            LinearLayout llRootCategory = (LinearLayout) view
                    .findViewById(R.id.categoryPickerRootCategoryButton);

            llRootCategory.setVisibility(View.GONE);
        }

        private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                mClickListener.onPageListItemSelcected((VenueCategory) mListAdapter.getItem(position));
            }
        };
    }

    private PageListItemSelected mPageListItemSelected = new PageListItemSelected() {
        @Override
        public void onPageListItemSelcected(VenueCategory category) {
            // If the item has children, create a new page for it.
            mChosenCategory = category;
            cancel();
        }

        @Override
        public void onCategorySelected(VenueCategory category) {
            // The user has chosen the category parent listed at the top of the
            // current page.
            mChosenCategory = category;
            cancel();
        }
    };

    private interface PageListItemSelected {
        public void onPageListItemSelcected(VenueCategory category);
        public void onCategorySelected(VenueCategory category);
    }
}
